// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_revive\xr_main.sqf"
#include "xr_macros.sqf"

if (isNil QGVARXR(with_marker)) then {
	GVARXR(with_marker) = true;
};

[0, QGVARXR(wn), {_this call FUNCXR(handlenet)}] call d_fnc_NetAddEvent;
[0, QGVARXR(setCap), {(_this select 0) setCaptive (_this select 1)}] call d_fnc_NetAddEvent;
[0, QGVARXR(a1), {_this switchMove "ainjpfalmstpsnonwrfldnon_carried_up"}] call d_fnc_NetAddEvent;
[0, QGVARXR(a2), {_this switchMove "acinpknlmstpsraswrfldnon_acinpercmrunsraswrfldnon"}] call d_fnc_NetAddEvent;
[0, QGVARXR(a3), {_this switchMove "AinjPpneMstpSnonWrflDnon"}] call d_fnc_NetAddEvent;
[0, QGVARXR(a4), {_this switchMove "unconscious"}] call d_fnc_NetAddEvent;
[0, QGVARXR(sd), {_this setDir 180}] call d_fnc_NetAddEvent;
[0, QGVARXR(a55), {_this switchMove "adthppnemstpsraswrfldnon_1"}] call d_fnc_NetAddEvent;
if (!isDedicated) then {
	[QGVARXR(m_msg), {
		if (isPlayer (_this select 0) && {(_this select 0) != (_this select 1)}) then {
			systemChat format [(localize "STR_DOM_MISSIONSTRING_911"), name (_this select 0), name (_this select 1)];
		} else {
			systemChat format [(localize "STR_DOM_MISSIONSTRING_912"), name (_this select 1)];
		};
	}] call d_fnc_NetAddEventToClients;
	[QGVARXR(cpr), {_this call FUNCXR(cpr)}] call d_fnc_NetAddEventSTO;
};

[QGVARXR(addpoint), {
	scriptName "ehxr_addpoint";
	private ["_killer", "_killed", "_par", "_par1", "_namep", "_namek", "_killedfriendly"];
	PARAMS_2(_killer,_killed);
	_killer addScore 1;
	#ifdef __TT__
	_par1 = d_player_store getVariable (getPlayerUID _killed);
	_namep = if (isNil "_par1") then {"Unknown"} else {_par1 select 6};
	_par = d_player_store getVariable (getPlayerUID _killer);
	_namek = if (isNil "_par") then {"Unknown"} else {_par select 6};
	_sidegrpk = side (group _killer);
	switch (_sidegrpk) do {
		case blufor: {
			d_points_blufor = d_points_blufor + (d_tt_points select 8);
			["d_u_k", [_namek, _namep, "BLUFOR"]] call d_fnc_NetCallEvent;
		};
		case opfor: {
			d_points_opfor = d_points_opfor + (d_tt_points select 8);
			["d_u_k", [_namek, _namep, "OPFOR"]] call d_fnc_NetCallEvent;
		};
	};
	if (d_domdatabase) then {
		_par set [20, (_par select 20) + 1];
	};
	#else
	if (d_domdatabase) then {
		_par1 set [29, (_par1 select 29) + 1];
	};
	[QGVARXR(m_msg), [_killer, _killed]] call d_fnc_NetCallEventToClients;
	#endif
}] call d_fnc_NetAddEventCTS;
[1, QGVARXR(subpoint), {
	scriptName "ehxr_subpoint";
	(_this select 0) addScore (d_sub_tk_points * -1);
	if (d_domdatabase) then {
		(_this select 0) call d_fnc_PAddTeamKillPoints;
	};
}] call d_fnc_NetAddEvent;

if (d_sub_kill_points != 0) then {
	if (d_sub_kill_points > 0) then {d_sub_kill_points = d_sub_kill_points * -1};
	[QGVARXR(add_sub_kill), {_this addScore d_sub_kill_points}] call d_fnc_NetAddEventCTS;
};

[QGVARXR(unconmsg), {
	systemChat format [(localize "STR_DOM_MISSIONSTRING_912"), name _this];
}] call d_fnc_NetAddEventToClients;
[2, QGVARXR(subpoint), {
	if (isPlayer (_this select 0)) then {
		systemChat format [(localize "STR_DOM_MISSIONSTRING_911"), name (_this select 0), name (_this select 1)];
	} else {
		systemChat format [(localize "STR_DOM_MISSIONSTRING_912"), name (_this select 1)];
	};
}] call d_fnc_NetAddEvent;
[QGVARXR(msg), {
	if ((_this select 2) == side (group player) && {(_this select 1) != GVARXR(name_player)}) then {
		systemChat format [(localize "STR_DOM_MISSIONSTRING_913"), _this select 1, _this select 0];
	};
	if ((_this select 1) == GVARXR(name_player)) then {
		_this spawn {
			sleep 2.5;
			systemChat (format [(localize "STR_DOM_MISSIONSTRING_914"), _this select 0]);
		};
	};
}] call d_fnc_NetAddEventToClients;
[0, QGVARXR(swmnon), {_this switchMove ""}] call d_fnc_NetAddEvent;
[0, QGVARXR(wn2), {if (local (_this select 0)) then {_this call FUNCXR(handlenet)}}] call d_fnc_NetAddEvent;
#ifdef __TT__
[QGVARXR(addActions), {if (player != (_this select 0) && {(_this select 1) == side (group player)}) then {(_this select 0) call FUNCXR(addActions)}}] call d_fnc_NetAddEventToClients; // TODO, what if more than one side is friendly
#else
[QGVARXR(addActions), {if (player != _this) then {_this call FUNCXR(addActions)}}] call d_fnc_NetAddEventToClients;
#endif
[QGVARXR(removeActions), {if (player != _this) then {_this call FUNCXR(removeActions)}}] call d_fnc_NetAddEventToClients;
[0, QGVARXR(hpl), {_this hideobject true}] call d_fnc_NetAddEvent;
[QGVARXR(grpl), {(_this select 0) selectLeader (_this select 1)}] call d_fnc_NetAddEventSTO;
[2, QGVARXR(delml), {
	_this setMarkerPosLocal [0,0,0];
	deleteMarkerLocal _this;
}] call d_fnc_NetAddEvent;
[0, QGVARXR(setgid), {(_this select 0) setGroupid [_this select 1]}] call d_fnc_NetAddEvent;

if (GVARXR(withSounds) && {!isDedicated}) then {
	GVARXR(moansoundsar) = [
		[
			"Person0",
			[
				"P0_moan_13_words.wss", "P0_moan_14_words.wss", "P0_moan_15_words.wss", "P0_moan_16_words.wss",
				"P0_moan_17_words.wss", "P0_moan_18_words.wss", "P0_moan_19_words.wss", "P0_moan_20_words.wss"
			]
		],
		[
			"Person1",
			[
				"P1_moan_19_words.wss", "P1_moan_20_words.wss", "P1_moan_21_words.wss", "P1_moan_22_words.wss",
				"P1_moan_23_words.wss", "P1_moan_24_words.wss", "P1_moan_25_words.wss", "P1_moan_26_words.wss",
				"P1_moan_27_words.wss", "P1_moan_28_words.wss", "P1_moan_29_words.wss", "P1_moan_30_words.wss",
				"P1_moan_31_words.wss", "P1_moan_32_words.wss", "P1_moan_33_words.wss"
			]
		],
		[
			"Person2",
			[
				"P2_moan_14_words.wss", "P2_moan_15_words.wss", "P2_moan_16_words.wss", "P2_moan_17_words.wss",
				"P2_moan_18_words.wss", "P2_moan_19_words.wss", "P2_moan_20_words.wss", "P2_moan_21_words.wss"
			]
		],
		[
			"Person3",
			[
				"P3_moan_10_words.wss", "P3_moan_11_words.wss", "P3_moan_12_words.wss", "P3_moan_13_words.wss",
				"P3_moan_14_words.wss", "P3_moan_15_words.wss", "P3_moan_16_words.wss", "P3_moan_17_words.wss",
				"P3_moan_18_words.wss", "P3_moan_19_words.wss", "P3_moan_20_words.wss"
			]
		]
	];
	[QGVARXR(say3d), {
		__TRACE_1("say3d","_this");
		if (alive player && {!__pGetVar(GVARXR(pluncon))} && {alive (_this select 0)} && {(_this select 0) getVariable QGVARXR(pluncon)} && {isNil QGVARXR(stopspect) || {!GVARXR(stopspect)}}) then {
			playSound3D ["a3\sounds_f\characters\human-sfx\" + ((GVARXR(moansoundsar) select (_this select 1)) select 0) + "\" + (((GVARXR(moansoundsar) select (_this select 1)) select 1) select (_this select 2)), _this select 0, false, _this select 3, 1, 1, 100];
		};
	}] call d_fnc_NetAddEventToClients;
};

if (GVARXR(with_marker)) then {
	//check markers
	if (isServer) then {
		GVARXR(uncon_m_units) = [];
	};
	[0, QGVARXR(umarker), {_this call FUNCXR(addmarker)}] call d_fnc_NetAddEvent;

	[2, QGVARXR(movemarker), {_this call FUNCXR(movemarker)}] call d_fnc_NetAddEvent;
};

if (isDedicated) exitWith {};

if (isNil QGVARXR(respawn_available_after)) then {GVARXR(respawn_available_after) = 150};
if (isNil QGVARXR(near_player_dist)) then {GVARXR(near_player_dist) = 250};
if (isNil QGVARXR(lifetime)) then {GVARXR(lifetime) = 300};
if (isNil QGVARXR(can_revive)) then {GVARXR(can_revive) = []};
if (isNil QGVARXR(withweaponrespawn)) then {GVARXR(withweaponrespawn) = true};

if (isNil QGVARXR(respawn_markers)) then {GVARXR(respawn_markers) = ["base"]};

GVARXR(dropAction) = -3333;
if (isNil QGVARXR(phd_invulnerable)) then {GVARXR(phd_invulnerable) = false};

GVARXR(respawn_delay) = getNumber(missionConfigFile/"respawndelay");

if (GVARXR(selfheals) > 0) then {
	0 spawn {
		scriptName "spawn_xr_selfheal";
		private "_id";
		while {true} do {
			__pSetVar [QGVARXR(numheals), GVARXR(selfheals)];
			waitUntil {alive player};
			_id = player addAction ["<t color='#FF0000'>Self Heal</t>", {_this call FUNCXR(selfheal)}, [], -1, false, false, "", "alive _target &&  {!(_target getVariable 'xr_pluncon')} && {!(_target getVariable 'xr_pisinaction')} && {(damage _target >= (xr_selfheals_minmaxdam select 0))} && {(damage _target <= (xr_selfheals_minmaxdam select 1))} && {(_target getVariable 'xr_numheals') > 0}"];
			waitUntil {!alive player};
			player removeAction _id;
		};
	};
};
